package chixing.day17;

import java.io.*;

public class FileCopyTest {
    public static File copy(File srcFile,String destPath){
        File destFile = new File(destPath);
        //1.创建字节读取流，写入流
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(srcFile);
            os =new FileOutputStream(destFile);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //2.边读边写
        int temp;
        try {
            while ((temp = is.read()) != -1){
                os.write(temp);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {//关闭流
            if (is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return destFile;
    }
}
